<template>
    <div class="login-container">
        <div class="login-box">
            <div class="avatar-box"><img src="../assets/logo.png" alt="logo"></div>
            <!--        表单登陆区域-->
            <el-form :model="login_form" :rules="login_formRules"
                     label-width="0px" class="login-form"
            ref="login_form">
                <el-form-item prop="username">
                    <el-input v-model="login_form.username"  prefix-icon="iconfont icon-user"></el-input>
                </el-form-item>
                <el-form-item prop="password">
                    <el-input v-model="login_form.password" prefix-icon="iconfont icon-3702mima"
                              type="password"></el-input>
                </el-form-item>
                <!--                按钮区域-->
                <el-form-item class="btns">
                    <el-button type="primary" @click="login">登陆</el-button>
                    <el-button type="info" @click="refreshBtn">重置</el-button>
                </el-form-item>
            </el-form>
        </div>
    </div>
</template>
<script>
    export default {
        name: 'login',
        components: {},
        data() {
            return {
                login_form: {
                    username: 'admin',
                    password: '123456',
                },
                login_formRules: {
                    //验证规则
                    username: [
                        {required: true, message: '请输入用户名',trigger: 'blur'},
                        { min: 3, max: 10, message: '用户名长度应在 3 到 10个字符', trigger: 'blur' }
                    ],
                    password: [
                        {required: true, message: '请输入用户密码',trigger: 'blur'},
                        { min: 6, max: 12, message: '密码长度应在 6 到 12个字符', trigger: 'blur' }
                        ],
                }
            }
        },
        methods:{
            refreshBtn(){
                this.$refs.login_form.resetFields()
            },
            //登陆按钮  检验验证结果
            login(){
                this.$refs.login_form.validate( async valid =>{
                    if(!valid) return;
                    const {data} = await this.$http.post('login',this.login_form)
                    if (data.meta.status !== 200)return  this.$message.error('登陆失败')
                    this.$message.success('登陆成功');
                    //将登陆成功后的token 保存在了客户端的sessionStorage中
                    //项目中出现登陆以外的API接口 ,需要登陆成功后才能访问
                    //token应该在当前网站打开时生效,所有将token 保存在sessionStrorage中
                    window.sessionStorage.setItem('token' , data.data.token)
                    //跳转到home界面
                    this.$router.push('/home')
                })
            }
        }

    }
</script>
<style lang="less" scoped>
    .login-container {
        background-color: #2b4b6b;
        height: 100%;
    }

    .login-box {
        width: 450px;
        height: 400px;
        background-color: white;
        border-radius: 3px;
        position: absolute;
        top: 50%;
        left: 50%;
        transform: translate(-50%, -50%);
    }

    .avatar-box {
        height: 130px;
        width: 130px;
        border: 1px solid #eeeeee;
        border-radius: 50%;
        padding: 10px;
        position: absolute;
        left: 50%;
        transform: translate(-50%, -50%);
        background-color: white;

        img {
            width: 100%;
            height: 100%;
            background-color: #eeeeee;
            border-radius: 50%;
        }
    }

    .login-form {
        position: absolute;
        bottom: 0;
        width: 100%;
        padding: 0 20px;
        box-sizing: border-box;

    }

    .btns {
        display: flex;
        justify-content: flex-end;
    }
</style> 